//
//  NibLoadable.swift
//  Bookkeeping
//
//  Created by 灵工邦 on 2024/5/6.
//

import UIKit

protocol NibLoadable {
    
}

extension NibLoadable where Self : UIView {
    //在协议里面不允许定义class 只能定义static
    static func loadFromNib(_ nibname: String? = nil) -> Self {//Self (大写) 当前类对象
        //self(小写) 当前对象
        let loadName = nibname == nil ? "\(self)" : nibname!
        
        return Bundle.main.loadNibNamed(loadName, owner: nil, options: nil)?.first as! Self
    }
    
    //在协议里面不允许定义class 只能定义static
    static func loadUINibName(_ nibname: String? = nil) -> Self {//Self (大写) 当前类对象
        //self(小写) 当前对象
        let loadName = nibname == nil ? "\(self)" : nibname!
        
        let nib = UINib.init(nibName: loadName, bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! Self
        return v
    }
}
